package servlet;

import it.polimi.swimv2.entities.User;
import it.polimi.swimv2.remote.FriendsManagerRemote;
import it.polimi.swimv2.remote.LoginManagerRemote;
import it.polimi.swimv2.util.RemoteBindingName;

import java.io.IOException;
import java.util.Collection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class FriendsServlet
 */
public class FriendsServlet extends HttpServlet {
	
	
	//Jsp pages
	private static final String FRIENDS_JSP = "friends.jsp";	
	private static final String ERROR_JSP = "error.jsp";
	private static final String INDEX_JSP = "index.jsp";
	private static final String LOGIN_FORM_JSP = "loginForm.jsp";
	
	//Paramaters of HTTP Request
	private static final String FRIENDS = "friends";
	private static final String USER = "user";
	private static final String REMOVE_FRIEND = "removeFriend";
	private static final String SHOW_FRIENDS = "showFriends";
	private static final String USERNAME = "username";
	private static final String ACT = "act";
	
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FriendsServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		chooseAction(request, response);
	}
	
	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	/**
	 * manages the action requested by http request
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void chooseAction(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		String act = request.getParameter(ACT);
		String friend = request.getParameter(USERNAME);
		HttpSession session = request.getSession();
		String username = (String) session.getAttribute(USER);
		if(username==null){
			response.sendRedirect(LOGIN_FORM_JSP);
			return;
		}
		try {
			FriendsManagerRemote rem = retrieveFriendsManagerRemote();
			if(act.equals(SHOW_FRIENDS)){			
				processShowFriends(request, response, rem, session);
			}else if(act.equals(REMOVE_FRIEND) && friend!=null){
				processDeleteFriend(request, response, friend, rem, session);
			}else{
				response.sendRedirect(INDEX_JSP);
			}
		} catch (NamingException e) {
			response.sendRedirect(ERROR_JSP);
		}
		
	}
	/**
	 * 
	 * @param request
	 * @param response
	 * @param friend
	 * @param rem
	 * @param session
	 * @throws ServletException
	 * @throws IOException
	 */
	private void processDeleteFriend(HttpServletRequest request, HttpServletResponse response, String friend, FriendsManagerRemote rem, HttpSession session) throws ServletException, IOException {
			rem.removeFriendship((String) session.getAttribute(USER), friend);
			processShowFriends(request, response, rem , session);		
	}
	/**
	 * shows the friends list
	 * @param request
	 * @param response
	 * @param rem
	 * @param session
	 * @throws ServletException
	 * @throws IOException
	 */
	private void processShowFriends(HttpServletRequest request, HttpServletResponse response, FriendsManagerRemote rem, HttpSession session) throws ServletException, IOException {
			Collection<User> friends= rem.getFriendsOf((String) session.getAttribute(USER));
			request.setAttribute(FRIENDS, friends);
			RequestDispatcher dispatcher = request.getRequestDispatcher(FRIENDS_JSP);
			dispatcher.forward(request, response);		
	}
	/**
	 * retrieves the remote object
	 * @return
	 * @throws NamingException
	 */
	private FriendsManagerRemote retrieveFriendsManagerRemote() throws NamingException {
		Context jndiContext = new InitialContext();
		Object ref = jndiContext.lookup(RemoteBindingName.FRIENDS_MANAGER);
		return (FriendsManagerRemote) ref;
	}

}
